package com.gitee.mars.vaccin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.vaccin.entity.DevelopPlace;
import com.gitee.mars.vaccin.request.VaccinNameRequest;
import com.gitee.mars.vaccin.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.vaccin.service.IVaccinNameService;
import com.gitee.mars.vaccin.mapper.VaccinNameMapper;
import com.gitee.mars.vaccin.entity.VaccinName;
import org.springframework.util.CollectionUtils;

import java.util.Date;


/**
 * VaccinName 实现层
 *
 * @Author wq
 * @Date 2021-04-11
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class VaccinNameServiceImpl extends ServiceImpl<VaccinNameMapper, VaccinName> implements IVaccinNameService {


    @Override
    public void add(VaccinNameRequest request) {
        VaccinName vaccinName = new VaccinName();
        vaccinName.setName(request.getName());
        vaccinName.setNameEnglish(request.getNameEnglish());
        vaccinName.setAbbr(request.getAbbr());
        vaccinName.setDescription(request.getDescription());
        vaccinName.setUpdateTime(new Date());
        vaccinName.setCreateTime(new Date());
        save(vaccinName);
    }

    @Override
    public PageInfo<VaccinName> list(VaccinNameRequest request) {
        IPage<VaccinName> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<VaccinName> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(request.getName())) {
            wrapper.like(VaccinName::getName, request.getName());
        }
        IPage<VaccinName> vaccinNameIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(vaccinNameIPage.getRecords())) {
            return new PageInfo<>(page.getTotal(), vaccinNameIPage.getRecords());
        }
        return null;
    }
}